% multiclassify  by D-FNN

clc,clear,close all

wake_feature = load('scale_wake', '-ascii');
n1_feature = load('scale_n1', '-ascii');
n2_feature = load('scale_n2', '-ascii');
n3_n4_feature = load('scale_n3', '-ascii');
r_feature = load('scale_r', '-ascii');

TOTAL_FEATURE_NUM = 29;
start_feature_NUM = 8;
end_FEATUURE_NUM = 29;
wake_feature = wake_feature(:,start_feature_NUM:end_FEATUURE_NUM);
n1_feature = n1_feature(:, start_feature_NUM:end_FEATUURE_NUM);
n2_feature = n2_feature(:, start_feature_NUM:end_FEATUURE_NUM);
n3_n4_feature = n3_n4_feature(:, start_feature_NUM:end_FEATUURE_NUM);
r_feature = r_feature(:,start_feature_NUM:end_FEATUURE_NUM);

train_wake_num = 200;
train_n1_num = 106;
train_n2_num = 200;
train_n3_n4_num = 200;
train_rem_num = 100;
test_wake_num = 50;
test_n1_num = 30;
test_n2_num = 150;
test_n3_n4_num = 100;
test_rem_num = 50;

x1 = n1_feature(1:train_n1_num, :)'; x2 = r_feature(1:train_rem_num, :)';
p = [x1 x2];
t = [ones(1, train_n1_num) 0 * ones(1, train_rem_num)];
[u_p, v_p] = size(p)
%Setting initial values
kdmax=0.5; kdmin=0.25;gama=0.9;beta=0.95;width0=1;
emax=1.1;emin=0.02;k=1.2;kw=1.1;kerr=0.025;
parameters(1)=kdmax;    parameters(2)=kdmin;    parameters(3)=gama;
parameters(4)=emax;     parameters(5)=emin;     parameters(6)=beta;
parameters(7)=width0;   parameters(8)=k;        parameters(9)=kw;
parameters(10)=kerr;

[w1, w2, width,rule, e, RMSE] = DFNN(p,t,parameters);
TA = RBF(dist(w1,p), 1./width');
TA0=sum(TA);
[u,v]=size(w1);
TA1=TA./(ones(u,1)*TA0);
TA2=transf(TA1,p);
outTA2_tmp=w2*TA2;
outTA2 = [outTA2_tmp > 0.5];
figure,plot(rule,'r');
title('Fuzzy rule eneration');
xlabel('Input sample patterns');
figure,plot(e,'r');
title('Actual output error');
xlabel('Input sample patterns');
figure,plot(RMSE,'r');
title('Root mean squared error(RMSE)');
xlabel('Input sample patterns');
figure;
k=1:v_p;
plot(k,t,'r-',k,outTA2,'ro');
title('Comparison between desired and actual outputs');
xlabel('Time t');

% test program
x1 = n1_feature(train_n1_num : train_n1_num + test_n1_num - 1, :)'; x2 = r_feature(train_rem_num : train_rem_num + test_rem_num - 1, :)';
ALLIN = [x1 x2];
[u_a, v_a] = size(ALLIN);
tt = [ones(1, test_n1_num) 0 * ones(1, test_rem_num)];
A = RBF(dist(w1,ALLIN), 1./width');
SA = sum(A); [u,v] = size(w1);
A1 = A./(ones(u,1)*SA);
A2 = transf(A1, ALLIN);
A3_tmp = w2*A2;
A3 = [A3_tmp > 0.5];

acc = 1 - sum(abs(A3 - tt))/ v_a
sse = sumsqr(tt - A3)/(500); rmse = sqrt(sse);
k = 1:v_a;
figure;
plot(k,tt,'r-',k,A3,'bo');
title('Comparison between desired and predicted outputs');
xlabel('Time t');
figure;
plot(k,tt-A3,'r');
title('Prediction error');
xlabel('Time t');